import { defineStore } from 'pinia'
import request from '../utils/request'
import { IChannelItem, IResponse } from '../types/data'

export default defineStore('channel', {
  state: () => ({
    channelList: [] as IChannelItem[],
    // #1 准备 active，表示激活项的 id
    active: -1,
  }),
  actions: {
    async getChannelList() {
      const r = await request.get<IResponse<{ channels: IChannelItem[] }>>('/channels')
      this.channelList = r.data.data.channels
      // #3 指定 active 为频道第 0 项的 id
      this.active = this.channelList[0].id
    },
    // #4 准备修改 active 的 action
    changeActive(active: number) {
      this.active = active
    },
  },
  getters: {},
})
